package com.baidu.encrypt.common.utils;

import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;

public class SM3Util {

    static {
        // 确保引入 BouncyCastleProvider
        if (java.security.Security.getProvider("BC") == null) {
            java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        }
    }

    /**
     * 计算SM3摘要
     * @param data 明文字符串
     * @return SM3摘要（Hex）
     */
    public static String encrypt(String data) {
        if (data == null) {
            data = "";
        }
        SM3Digest digest = new SM3Digest();
        byte[] srcData = data.getBytes();
        digest.update(srcData, 0, srcData.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return Hex.toHexString(hash);
    }
}
